# -*- coding: utf-8 -*-
import os
import shutil

root_path = '/home/hujiahui/git/paper/dataset'#cases目录
software_name = 'innovus'
file_names = os.listdir(root_path)
print(file_names)
libs =['asap7']
for file in file_names:
    for lib in libs:
        run_tcl_path = f'{root_path}/{file}/{lib}/{software_name}/'

        # 指定要保留的关键子文件夹和子文件
        key_subfolder = 'log' #'log'
        key_file = ['run.tcl', 'run.log', 'mmmc.tcl']  #'run.tcl'    ['run.tcl', 'genus_constraint.sdc']
        try:
            # 获取文件夹内的所有内容
            contents = os.listdir(run_tcl_path)

            # 遍历文件夹内容，保留关键子文件夹和子文件，删除其他内容
            for item in contents:
                item_path = os.path.join(run_tcl_path, item)
                #如果不是指定要保留的文件夹和文件，则将其删除
                if item != key_subfolder and item not in key_file:
                    if os.path.isdir(item_path):
                        shutil.rmtree(item_path)  # 删除子文件夹及其内容
                    else:
                        os.remove(item_path)  # 删除子文件

        except:
            print(f'{run_tcl_path} dont exist')

        print(f"Only the key subfolder and file are retained in '{file}'.")

print("\nTask completed.")




